﻿package com.mod.page  {
	
    import com.calefk.core.Page;
    import com.calefk.util.Logger;
    import com.mod.engine.Engine;
    import com.mod.Global;
    import com.mod.ModProject;
    import com.mod.PMGame;
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
	
	public class GamePage extends Page {
		
		public function GamePage(canvas:MovieClip, nameClip:String = null, x:Number = 0, y:Number = 0) {
			super(canvas, nameClip, x, y);
            // Create engine
			Global.stopAllSounds();
			
			Global.engine = new Engine(this , mClip);
			Global.playSound("bgm_battle_1", int.MAX_VALUE);
		}
        
        public function initPage():void {
            trace("::: Game Init Page :::");
        }
		
		override public function update(dt:int):void {
            // update all popups
            super.update(dt);
            
            // update engine
			if (Global.engine == null) { return;  }
			
			if (Global.engine.toClean) {
				Global.engine.clean();
				Global.engine = null;
				// call function end clean
				endclean();
			} else {
				Global.engine.update(dt);
			}
		}
        
        override public function onKeyDown(e:KeyboardEvent):void {
            super.onKeyDown(e);
            if (Global.engine) {
                Global.engine.onKeyDown(e);
            }
        }
        
        override public function onKeyUp(e:KeyboardEvent):void {
            super.onKeyUp(e);
            if (Global.engine) {
                Global.engine.onKeyUp(e);
            }
        }
		
		public function endclean():void {
			Logger.echo("::: End clean :::");
			Global.stopAllSounds();
			ModProject.getPM().gotoPage(PMGame.PAGE_OVER);
		}
		
		override public function clean():void {
			if (Global.engine) {
				Global.engine.clean();
				Global.engine = null;
			}
			super.clean();
		}
		
	}

}